package TwentyOne.Class.Map;


/*
 一对多映射
 分类：分类ID、名称
 子分类：子分类ID、分类ID、名称

 获取一个分类的完整信息；
 可以根据分类获取其对应的所有子分类的信息。
 */

// 分类 和 子分类
class Item {

    private long iid;
    private String title;
    private Subitem subitems[];

    public Item(long iid, String title) {
        this.iid = iid;
        this.title = title;
    }

    public void setSubitems(Subitem subitems[]) {
        this.subitems = subitems;
    }

    public Subitem[] getSubitems() {
        return this.subitems;
    }

    public String getInfo() {
        return "【分类信息】iid =" + this.iid + "、title = " + this.title;
    }
}

class Subitem {

    private long sid;
    private String title;
    private Item item;

    public Subitem(long sid, String title) {
        this.sid = sid;
        this.title = title;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public Item getItem() {
        return this.item;
    }

    public String getInfo() {
        return "【子分类信息】sid = " + this.sid + "、title = " + this.title;
    }
}

public class a11_MapOneToMany {

    public static void main(String[] args) {
        // 根据结构设置对象数据
        Item item = new Item(1, "图书");
        Subitem subitems[] = new Subitem[]{
            new Subitem(10, "编程图书"),
            new Subitem(10, "图形图像类图书")
        };
        item.setSubitems(subitems);
        for (int x = 0; x < subitems.length; x++) {
            subitems[x].setItem(item);
        }
        // 根据要求获取数据
        System.out.println(item.getInfo());
        for (int x = 0; x < item.getSubitems().length; x++) {
            System.out.println("\t|- " + item.getSubitems()[x].getInfo());
        }
    }
}
